<?php
/**
 * 
 * @author spring
 * @version 2014-6-7 上午9:37:58
 */
class  Controller_Rate extends Controller_Admin {
/**
 *汇率的新增查询与修改
 *
 */
    public function before()
    {
    	try {
    		  ACL::Required('rate');
    	} catch ( HTTP_Exception_403 $e ) {
				Message::error('sorry，你没有权限进这个目录');
    		$this->request->redirect ( URL::base (true) );
    	}
        parent::before();
    }
	/**
	 * 
	 * Description
	 * 添加汇率界面
	 */
	public function action_index()
 	{
 		$this->title='添加汇率';
 		$content=View::factory("delivery/rate");
 		$this->response->body($content);
 	   if(Basic::is_post()){
 	   	$condition=$_POST;
 	   	if(Rate::insert_rate($condition)){
 	   		$jsonarr=array('status'=>'Y','info'=>'提交成功');
 	   	 }
 	   	else {
 	   		$jsonarr=array('status'=>'N','info'=>'提交失败');
 	    }
 	     echo json_encode($jsonarr);
 	     exit();
 	   }
 	}
 	/**
 	 * 
 	 * Description
 	 *汇率查询界面
 	 */
 	public function action_search(){
 		
 		$this->title='汇率查询修改';
 		$content=View::factory("delivery/rate_search");
 		$this->response->body($content);
 	}
 	/**
 	 * 
 	 * Description
 	 * ajxa 执行汇率搜索
 	 */
 	public function action_searchto()
 	{
 		$tablename='rates';
 		$result=$this->fenye($tablename);
 		$view=View::factory('delivery/rate_table')->set('datas',$result['datas'])->bind('pager_links', $result['pager_links'])->render();
 		echo $view;
 	
 	}
 	
 	/**
 	 * 
 	 * Description
 	 * ajax 执行汇率修改
 	 */
 	public function action_edit()
 	{
 		$condition=$_GET;
 		$total_rows=Rate::editRate($condition);
 		
 	}
 	

 	public function action_down_temple()
 	{
 		$tablename='rates';
 		$condition=array('down_temple'=>true);
 		set_time_limit(0);
 		ini_set('memory_limit','512M');
 		require_once(Kohana::find_file('vendor', 'PHPExcel/PHPExcel/IOFactory'));
 		$this->objReader  = PHPExcel_IOFactory::createReader('Excel5');
 		$objPHPExcel      = new PHPExcel();
 		$objPHPExcel->getProperties()->setCreator("ROMWE")
 		->setLastModifiedBy("ROMWE")
 		->setTitle("Office 2003 XLSX Test Document")
 		->setSubject("Office 2003 XLSX Test Document")
 		->setDescription("Test document for Office 2003 XLSX, generated using PHP classes.")
 		->setKeywords("office 2003 openxml php")
 		->setCategory("Test result file");
 		$objActSheet =$objPHPExcel->setActiveSheetIndex(0);
 		$objActSheet->setCellValue('A1', '币种');
 		$objActSheet->setCellValue('B1', '人民币汇率');
 		$objActSheet->setCellValue('C1', '美元汇率');
 		$pre= Rate::getListInfoCondition(Rate::getListPre($tablename),$condition,true,$tablename);
 		$datas=Rate::getList($items_per_page,$offset,$pre);
 		$time=date('Y-m-d H:i:s');
 		for($i=0;$i<count($datas);$i++)
 		{
 		$key=$i+2;
 		$objActSheet->setCellValue('A'.$key, $datas[$i]['name']);
 		$objActSheet->setCellValue('B'.$key, $datas[$i]['rate1']);
 		$objActSheet->setCellValue('C'.$key, $datas[$i]['rate2']);
 		}
 		$objPHPExcel->getActiveSheet()->setTitle('汇率报表');
 				$objPHPExcel->setActiveSheetIndex(0);
 				$day=date("m-d");
 						$filename=$day.'汇率上传模版.xls';
        ob_end_clean();
 								header("Content-Type: application/vnd.ms-excel; charset=utf-8");
 								header('Content-Disposition: attachment;filename='.$filename);
 								header('Cache-Control: max-age=0');
 								$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
 								$objWriter->save('php://output');
 								exit;
 			
 	}
 	
 	public function action_upload_rate()
 	{
 		$this->title='汇率添加';
 		set_time_limit(0);
 		ini_set('memory_limit','500M');
 		require_once(Kohana::find_file('vendor', 'PHPExcel/PHPExcel/IOFactory'));
 		$xls= $_FILES['xls']['tmp_name'];
	 	$objPHPExcel= PHPExcel_IOFactory::load($xls);
	 	$data= $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
	 	Rate::check_rate_upload($data);
	 	$lines=Rate::do_rate_upload($data);
	 	$content=View::factory("delivery/special_upload_result")
	 	->set('lines',$lines);
	 	$this->response->body($content);
 	}
 	
 	
 	/**
 	 * 
 	 * Description 分页简单封装
 	 * @param field_type  bare_field_name
 	 * @param string $tablename
 	 * @return array  分页数据和视图
 	 */
 	private function fenye($tablename)
 	{
 		$condition=$_GET;
 		$items_per_page=10;
 		$pre= Rate::getListInfoCondition(Rate::getTotlePre($tablename),$condition,false,$tablename);
 		$pagination = Pagination::factory(array(
 				'total_items' =>Rate::getTotle($pre),
 				//我重新修改了分页视图
 				'view' => 'pagination/ajax_basic',
 				'items_per_page' => $items_per_page,
 				'uri'=>$this->request->uri(),
 		));
 		if(isset($_GET['page'])&&!empty($_GET['page']))
 		{
 			$offset=($_GET['page']-1)*$items_per_page;
 		}else {
 			$offset=0;
 		}
 		$pre= Rate::getListInfoCondition(Rate::getListPre($tablename),$condition,true,$tablename);
 		$datas=Rate::getList($items_per_page,$offset,$pre);
 		$pager_links = $pagination->render();
 		$result=array('datas'=>$datas,'pager_links'=>$pager_links);
 		return $result;
 	}
 	
}
